<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工管理</title>
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
	var url;
	//查询员工
	function searchEmployee() {
		$("#dg").datagrid('load', {
			"name" : $("#s_name").val(),
			"station" : $("#s_station").val()
		});
	}
	//添加员工
	function openAddEmployeeDialog() {
		resetValue();
		$("#employeeForm").dialog("open").dialog("setTitle", "添加员工");
		url = "${pageContext.request.contextPath}/employee/save.do";
	}
	//保存员工信息到后台
	function saveEmployee() {
		$("#fm").form("submit", {
			url : url,
			onSubmit : function() {
				return $(this).form("validate");
			},
			success : function(result) {
				var result = eval('(' + result + ')');
				if (result.success) {
					$.messager.alert("系统提示", "保存成功！");
					resetValue();
					$("#employeeForm").dialog("close");
					$("#dg").datagrid("reload");
				} else {
					$.messager.alert("系统提示", "保存失败！");
					return;
				}
			}
		});
	}
	//更新员工信息
	function openUpdateEmployeeDialog() {
		var selectedRows = $("#dg").datagrid("getSelections");
		if (selectedRows.length != 1) {
			$.messager.alert("系统提示", "请选择一个要修改的员工！");
			return;
		}
		var row = selectedRows[0];
		$("#employeeForm").dialog("open").dialog("setTitle", "修改员工信息");
		$("#fm").form("load", row);
		url = "${pageContext.request.contextPath}/employee/save.do?id="
				+ row.id;
	}

	//重置对话框数据为空
	function resetValue() {
		$("#name").val("");
		$("#sex").val("");
		$("#phone").val("");
		$("#idnum").val("");
		$("#major").val("");
		$("#eduback").val("");
		$("#station").val("");
		$("#position").val("");
		$("#skillrank").val("");
		$("#position").val("");
	}
	//关闭对话框
	function closeEmployeeDialog() {
		$("#employeeForm").dialog("close");
		resetValue();
	}
	//删除员工
	function deleteEmployee() {
		var selectRows = $("#dg").datagrid("getSelections");
		if (selectRows.length == 0) {
			$.messager.alert("系统提示", "请选择需要删除的行数据！");
			return;
		}
		var strIds = [];
		for (var i = 0; i < selectRows.length; i++) {
			strIds.push(selectRows[i].id);
		}
		var ids = strIds.join(",");
		$.messager.confirm("系统提示", "您确认要删除这<font color=red>"
				+ selectRows.length + "</font>条数据吗？", function(r) {
			if (r) {
				$.post("${pageContext.request.contextPath}/employee/delete.do",
						{
							ids : ids
						}, function(result) {
							if (result.success) {
								$.messager.alert("系统提示", "数据已成功删除！");
								$("#dg").datagrid("reload");
							} else {
								$.messager.alert("系统提示", "删除失败,请联系管理员！");
							}
						}, "json");
			}
		});
	}

	function formatPosition(val, row) {
		
		console.log(row);
		if (val != null) {
			return row.position.name;
		} else {
			return null;
		}
	}

	function formatStation(val, row) {
		if (val != null) {
			return row.station.name;
		} else {
			return null;
		}
	}

	function formatCompany(val, row) {
		if (val != null) {
			return row.compnay.name;
		} else {
			return null;
		}
	}

	function eduFormat(val, row) {
		if (val != null) {
			alert(val);
		}
	}
</script>
</head>
<body style="margin: 1px">
	<table id="dg" title="人员管理" class="easyui-datagrid" fitColumns="true"
		pagination="true" rownumbers="true"
		url="${pageContext.request.contextPath}/employee/list.do" fit="true"
		toolbar="#tb">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<th field="id" width="20" align="center">员工编号</th>
				<th field="name" width="30" align="center">姓名</th>
				<th field="sex" width="30" align="center">性别</th>
				<th field="phone" width="30" align="center">手机号</th>
				<th field="idnum" width="30" align="center">省份证号</th>
				<th field="major" width="30" align="center">专业</th>
				<th field="eduback" width="30" align="center">学历</th>
				<th field="station" formatter="formatStation" width="30"
					align="center">岗位</th>
				<th field="position" formatter="formatPosition" width="30"
					align="center">职位</th>
				<th field="skillrank" width="30" align="center">技能等级</th>
				<th field="hiredate" width="50" align="center">入职时间</th>
			</tr>
		</thead>
	</table>
	<div id="tb">
		<div>
			<a href="javascript:openAddEmployeeDialog()"
				class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a> <a
				href="javascript:openUpdateEmployeeDialog()"
				class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a><a
				href="javascript:deleteEmployee()" class="easyui-linkbutton"
				iconCls="icon-remove" plain="true">删除</a>
		</div>
		<div>
			&nbsp;姓名&nbsp;<input type="text" id="s_name" size="20"
				onkeydown="if(event.keyCode==13) searchBlog()" /> &nbsp;岗位&nbsp;<input
				type="text" id="s_station" size="20"
				onkeydown="if(event.keyCode==13) searchBlog()" /> <a
				href="javascript:searchEmployee()" class="easyui-linkbutton"
				iconCls="icon-search" plain="true">搜索</a>
		</div>
	</div>
	<div id="employeeForm" class="easyui-dialog"
		style="width: 500px; height: 680px; padding: 10px 20px" closed="true"
		buttons="#dlg-buttons">
		<form id="fm" method="post">
			<table cellspacing="8px">
				<tr>
					<td>姓名：</td>
					<td><input type="text" id="name" name="name"
						class="easyui-validatebox" required="true" /></td>
				</tr>
				<tr>
					<td>性别：</td>
					<td>男<input type="radio" name="sex" value="1" />女<input
						type="radio" name="sex" value="0" checked="checked" /></td>
				</tr>
				<tr>
					<td>手机号：</td>
					<td><input type="text" id="phone" name="phone"
						class="easyui-numberbox" required="true" /></td>
				</tr>
				<tr>
					<td>身份证号：</td>
					<td><input type="text" id="idnum" name="idnum" required="true" /></td>
				</tr>
				<tr>
					<td>专业：</td>
					<td><input type="text" id="major" name="major" /></td>
				</tr>
				<tr>
					<td>学历：</td>

					<td><select id="edubackSelect" formatter="eduFormat"
						name="eduback">
							<option value="0">-请选择岗位</option>
							<option value="本科">本科</option>
							<option value="专科">专科</option>
							<option value="其他">其他</option>
					</select></td>

				</tr>
				<tr>
					<td>岗位：</td>
					<td><select id="stationSelect" name="station">
							<option value="0">-请选择岗位</option>
							<c:forEach var="stationList" items="${stationList}">
								<option value="${ stationList.id}">${ stationList.name}</option>
							</c:forEach>
					</select></td>
				</tr>
				<tr>
					<td>职位：</td>
					<td><select id="positionSelect" name="position">
							<option value="0">-请选择岗位</option>
							<c:forEach var="positionList" items="${positionList}">
								<option value="${ positionList.id}">${ positionList.name}</option>
							</c:forEach>
					</select></td>
				</tr>
				<tr>
					<td>技能等级：</td>
					<td><input type="text" id="skillrank" name="skillrank" /></td>
				</tr>
				<tr>
					<td>入职时间：</td>
					<td><input class="easyui-datebox" id="hiredate"
						name="hiredate" editable=false />(请点击日期按钮完成日期输入)</td>
				</tr>

			</table>
		</form>
	</div>

	<div id="dlg-buttons">
		<a href="javascript:saveEmployee()" class="easyui-linkbutton"
			iconCls="icon-ok">保存</a> <a href="javascript:closeEmployeeDialog()"
			class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>
</body>
</html>